package com.example.textroom;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import java.io.File;

/**
 * Description:
 * Author: dengpao
 * Date: 2025/8/27 09:42
 */
public class TextActivity extends AppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text_activity);


        findViewById(R.id.btnAddDownload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String s1 = "https://httpbin.org/bytes/10485760";

                String s = "http://app_admin.xubo.cn/uploads/20250824/438cf583134617da591105e14a0e4676.apk";

                DownloadUtils downloadUtils=new DownloadUtils();
                downloadUtils.downloadFile(TextActivity.this,s, "file.apk", new DownloadUtils.DownloadCallback() {
                    @Override
                    public void onSuccess(String filePath) {
//                        openDownloadedFile(filePath);
                        File outputFile =new File(filePath, "file.apk");
                        installApp(TextActivity.this,outputFile);
                    }

                    @Override
                    public void onError(String error) {
                        Log.e("AAA",error);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
            }
        });


    }


    public  void installApp(Context context, File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        //版本在7.0以上是不能直接通过uri访问的
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // 由于没有在Activity环境下启动Activity,设置下面的标签
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致   参数3  共享的文件
            Uri apkUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", file);
            //添加这一句表示对目标应用临时授权该Uri所代表的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(file),
                    "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }

    private void openDownloadedFile(String filePath) {
        File file = new File(filePath);
        Uri fileUri = FileProvider.getUriForFile(this,
                getApplicationContext().getPackageName() + ".provider", file);

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(fileUri, getMimeType(filePath));
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this, "没有找到可以打开此文件的应用程序", Toast.LENGTH_SHORT).show();
        }
    }

    private String getMimeType(String filePath) {
        String type = null;
        String extension = filePath.substring(filePath.lastIndexOf(".") + 1);
        if (extension != null) {
            type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
        }
        return type != null ? type : "*/*";
    }
}
